<?php

namespace App\Repository\Commit;

use App\Models\Position as model;
use App\Repository\BaseRepository;

class GetConfigRepository extends  BaseRepository
{
    public function __construct(model $model)
    {
        $this->model = $model;
        parent::__construct();
    }
    public function index($request)
    {
        if (!empty($request['positionCode'])) {
            $cron['where']['position_code'] = $request['positionCode'];
        }
        if (!empty($request['created_start']) && !empty($request['created_end'])) {
            $cron['between']['create_time'][] = $request['created_start'];
            $cron['between']['create_time'][] = $request['created_end'];
        }
        if (!empty($request['status'])) {
            $cron['where']['status'] = $request['status'];
        }
        if (!empty($request['positionName'])) {
            $cron['like'] = ['position_name' => $request['positionName']];
        }
        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'position_id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $pageNums = 0;
        $cron['limit']['number'] = isset($request['pageSize']) ? ($request['pageSize']) : 10;
        if (isset($request['pageNum']) && !empty(($request['pageNum']))) {
            $pageNums = ($request['pageNum'] - 1) * $cron['limit']['number'];
        }
        $cron['limit']['start'] = $pageNums;
        // var_dump($request['user']['role_id'], 11111111111);
        if ($request['user']['role_id'] != 1) {
            $cron['where']['shop_id'] = $request['user']['shop_id'];
        }

        $data = $this->search($this->model, $cron);
        if (!empty($data['data'])) {
            $data['data'] = usfirstArr($data['data']);
            foreach ($data['data'] as $key => &$v) {
                if (!empty($v['roleId'])) {
                    $v['roleId'] = explode(',', $v['roleId']);
                }
            }
        }
        return $data;
        // return $this->search($this->model, $cron);
    }

    public function store($request)
    {
        $mustField = [
            'position_name',
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'position_name' => '名称不能为空'
        ];

        if ($this->model->where(['position_name' => $request->get('position_name'), 'shop_id' => $request->user->id])->first()) {
            return $this->returnMsg(-1, '该数据已添加');
        }
        $request->offsetSet('create_time', date('Y-m-d H:i:s'));
        $insertData  = $this->getRealData($request, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        $insertData['shop_id'] = $request->user->id;
        if (!empty($insertData['role_id'])) {
            // $resRole = json_decode($insertData['role_id'], true);
            $insertData['role_id'] = implode(',', $insertData['role_id']);
        }
        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    public function save($request, $id)
    {
        if (!empty($request['position_name'])) {
            $count = $this->model->where(['position_name' => $request['position_name']])->count();
            if ($count > 1) {
                return $this->returnMsg(-1, '该数据已添加');
            }
        }
        $userRes = $this->model->where('position_id', $id)->first();
        if (!$userRes) {
            return $this->returnMsg(-1, '未查询到该数据');
        }
        $update = [];
        foreach ($this->model->getFillable() as $v) {
            if (isset($request[$v]) && $request[$v] != '') {
                $update[$v] = $request[$v];
            }
        }
        $update['update_time'] = date('Y-m-d H:i:s');
        if (!empty($update['role_id'])) {
            // var_dump($update['role_id']);
            // $resRole = json_decode($update['role_id'], true);
            $update['role_id'] = implode(',', $update['role_id']);
        }
        $this->model->where('position_id', $id)->update($update);
        return $this->returnMsg(200, '修改成功', true);
    }

    public function show($id)
    {
        $res = $this->model->where('position_id', $id)->first();
        return $this->returnMsg(0, '获取成功', $res);
    }

    public function destroy($id)
    {
        $this->model->destroy($id);
        return $this->returnMsg(0, '删除成功', true);
    }
    public function oneSel($type)
    {
        $where = [
            'dict_type' => $type,
            'status' => 0,
        ];
        $resData = $this->model->where($where)->get();
        $returnData = [];
        if (!empty($resData)) {
            $returnData = usfirstArr($resData);
            if (!empty($returnData['roleId'])) {

                $returnData['roleId'] = explode(',', $returnData['roleId']);
            }
        }
        return $this->returnMsg(200, '查询成功', $returnData);
    }
    public function getOne($where)
    {
        return $this->model->where($where)->first();
    }
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }
    public function getAllData($where = array())
    {
        if (empty($where)) {
            $where[] = ['status', '=', 0];
        }
        $data =  $this->model->where($where)->get();
        $return = [];
        if (!empty($data)) {
            foreach ($data as $key => $v) {
                $return[] = usfirstArr($v);
            }
        }
        return $return;
    }
}
